<?php
// +----------------------------------------------------------------------
// | Description: 合同
// +----------------------------------------------------------------------
// | Author:  Michael_xu | gengxiaoxu@5kcrm.com
// +----------------------------------------------------------------------
namespace app\crm\model;

use app\admin\model\Record;
use app\admin\model\User;
use think\Db;
use app\admin\model\Common;
use think\Request;
use think\Validate;

class Assignfield extends Common
{
    protected $name = 'crm_assignfield';
    protected $createTime = 'butler_time';
    protected $updateTime = '';
    protected $autoWriteTimestamp = true;
    /**
     * [getDataList 外勤list]
     * @author Michael_xu
     * @param     [string]                   $map [查询条件]
     * @param     [number]                   $page     [当前页数]
     * @param     [number]                   $limit    [每页数量]
     * @return    [array]                    [description]
     */
    public function getDataList($request)
    {
        $user_id = $request['user_id'];

        $where=[];
        //$where=["contract_id"=>$request["contract_id"]];

        //列表展示字段
        // $indexField = $fieldModel->getIndexField('crm_contract', $user_id);

        $order = ['contract.create_time desc'];
        $list = db('admin_user')
            ->alias('admin')
            ->join('__CRM_CONTRACT__ contract',"FIND_IN_SET(admin.id,REPLACE(rw_user_id,',',','))")
            ->where($where)
            ->limit(($request['page']-1)*$request['limit'], $request['limit'])
            ->field('admin.*,contract.name contract_name,contract.num contract_num,contract.contract_id')
            ->order($order)
            ->select();
        $dataCount = db('admin_user')
            ->alias('admin')
            ->join('__CRM_CONTRACT__ contract',"FIND_IN_SET(admin.id,REPLACE(rw_user_id,',',','))")
            ->where($where)
            ->count('admin.id');

        //新增 统计合同金额和回款金额 end
        $data = [];
        $data['list'] = $list;
        $data['dataCount'] = $dataCount ? : 0;
        /*$data['data']['sumMoney'] = $sumMoney ? : 0.00;
        $data['data']['unReceivablesMoney'] = $unReceivablesMoney ? : 0.00;*/
        return $data;
    }


    /*
     *
     * */
    public function assign($data)
    {
        $res=$this->allowField(true)->saveAll($data);
        return $res;
    }
}